package com.ruoyi.purchase.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.SysGroupUser;
import com.ruoyi.purchase.service.ISysGroupUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 分组用户关联Controller
 *
 * @author ruoyi
 * @date 2025-08-23
 */
@RestController
@RequestMapping("/purchase/user")
public class SysGroupUserController extends BaseController
{
    @Autowired
    private ISysGroupUserService sysGroupUserService;

    /**
     * 查询分组用户关联列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysGroupUser sysGroupUser)
    {
        startPage();
        List<SysGroupUser> list = sysGroupUserService.selectSysGroupUserList(sysGroupUser);
        return getDataTable(list);
    }

    /**
     * 导出分组用户关联列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:user:export')")
    @Log(title = "分组用户关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysGroupUser sysGroupUser)
    {
        List<SysGroupUser> list = sysGroupUserService.selectSysGroupUserList(sysGroupUser);
        ExcelUtil<SysGroupUser> util = new ExcelUtil<SysGroupUser>(SysGroupUser.class);
        util.exportExcel(response, list, "分组用户关联数据");
    }

    /**
     * 获取分组用户关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('purchase:user:query')")
    @GetMapping(value = "/{groupId}")
    public AjaxResult getInfo(@PathVariable("groupId") Long groupId)
    {
        return success(sysGroupUserService.selectSysGroupUserByGroupId(groupId));
    }

    /**
     * 新增分组用户关联
     */
    @PreAuthorize("@ss.hasPermi('purchase:user:add')")
    @Log(title = "分组用户关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysGroupUser sysGroupUser)
    {
        return toAjax(sysGroupUserService.insertSysGroupUser(sysGroupUser));
    }

    /**
     * 修改分组用户关联
     */
    @PreAuthorize("@ss.hasPermi('purchase:user:edit')")
    @Log(title = "分组用户关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysGroupUser sysGroupUser)
    {
        return toAjax(sysGroupUserService.updateSysGroupUser(sysGroupUser));
    }

    /**
     * 删除分组用户关联
     */
    @PreAuthorize("@ss.hasPermi('purchase:user:remove')")
    @Log(title = "分组用户关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{groupIds}")
    public AjaxResult remove(@PathVariable Long[] groupIds)
    {
        return toAjax(sysGroupUserService.deleteSysGroupUserByGroupIds(groupIds));
    }
}
